package com.hedl.classroom.controller;

import com.hedl.classroom.base.exception.OnlinecClassroomException;
import com.hedl.classroom.base.model.PageResult;
import com.hedl.classroom.dto.MyCourseTableParams;
import com.hedl.classroom.dto.XcChooseCourseDto;
import com.hedl.classroom.dto.XcCourseTablesDto;
import com.hedl.classroom.po.XcCourseTables;
import com.hedl.classroom.service.MyCourseTablesService;
import com.hedl.classroom.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Big Watermelon
 * @description 我的课程表接口
 * @date 2023/12/16/0:19
 */
@Api(value = "我的课程表接口", tags = "我的课程表接口")
@Slf4j
@RestController
public class MyCourseTablesController {

    @Resource
    private MyCourseTablesService myCourseTablesService;

    @ApiOperation("添加选课")
    @PostMapping("/choosecourse/{courseId}")
    public XcChooseCourseDto addChooseCourse(@PathVariable("courseId") Long courseId) {
        //当前登陆用户，这里一般不用判断的因为，在网关哪里就基本被全部拦截了；但是目前我们是把白名单全部打开的
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        if (user == null){
            OnlinecClassroomException.cast("请登录！");
        }
        //用户id
        String userId = user.getId();

        //添加选课
        XcChooseCourseDto xcChooseCourseDto = myCourseTablesService.addChooseCourse(userId, courseId);

        return xcChooseCourseDto;
    }

    @ApiOperation("查询学习资格")
    @PostMapping("/choosecourse/learnstatus/{courseId}")
    public XcCourseTablesDto getLearnstatus(@PathVariable("courseId") Long courseId) {
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        if (user == null){
            OnlinecClassroomException.cast("请登录！");
        }
        //用户id
        String userId = user.getId();
        XcCourseTablesDto learningStatus = myCourseTablesService.getLearningStatus(userId, courseId);
        return learningStatus;

    }

    @ApiOperation("我的课程表")
    @GetMapping("/mycoursetable")
    public PageResult<XcCourseTables> mycoursetable(MyCourseTableParams params) {
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        if (user == null){
            OnlinecClassroomException.cast("请登录！");
        }
        //用户id
        String userId = user.getId();
        params.setUserId(userId);
        PageResult<XcCourseTables> pageResult = myCourseTablesService.mycourseTables(params);
        return pageResult;
    }
}
